// new VConsole();


new Vue({
    el: '#container',
    data: {
        formData: {
            id: '',
            contractId: "",
            metaInfo: {},
            certifyId:'',
        },
        response: {},  //认证后带的参数
        isSuccess: 0, // 0 为识别  1失败 2成功
        errMes: '很遗憾，识别失败！',
    },

    mounted() {
        let url = window.location.href.split('response=')[1];
        this.response = decodeURIComponent(url);
        //认证后返回
        if (this.response != 'undefined') {
            this.response = JSON.parse(this.response);
            //1003识别中断 （暂不认证）
            if (this.response.code == 1003) return;
            console.log('------------', this.response)
            //查询认证结果
            this.getData();

        } else {
            //首次进入带id和合同号
            let  contractId= this.GetQueryString('contractId');
            let id = this.GetQueryString('id');
                // console.log('11',contractId)
            if(contractId && id){
               Cookies.set('contractId',contractId);
               Cookies.set('id',id);
            }
            // console.log('data:',  this.formData)

        }

    },
    methods: {
        //匹配参数
        GetQueryString(name) {
            var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
            var r = window.location.search.substr(1).match(reg);
            if (r != null)
                return unescape(r[2]);
            return "";
        },
        //确认签署
        submit() {
            axios({
                url: baseUrl + '/signApp/contract/confirm/'+this.formData.contractId+'/'+this.formData.id,
                headers: {
                    "Content-Type": "application/json",
                },
                method: 'get',
            }).then(res=>{
                if(res.data.code==200){
                    wx.miniProgram.navigateTo({url: `/pages/contract-signed/index?contractNumber=${this.formData.contractId}`})
                }else {
                    alert(res.data.message);
                }
            })
        },
        // 查询识别结果
        getData() {
            //认证成功有返回值
            this.formData.contractId=Cookies.get('contractId');
            this.formData.id=Cookies.get('id');
            this.formData.certifyId=this.response.extInfo.certifyId
            axios({
                url: baseUrl + '/signApp/customer/faceCheck',
                headers: {
                    "Content-Type": "application/json",
                },
                method: 'post',
                data: this.formData,

            }).then(res => {
                let data = res.data;
                if (data.code == 200) {
                    this.isSuccess = 2;
                } else {
                    this.isSuccess = 1;
                    this.errMes = data.message;

                }

            })
        },
        //发起人脸识别
        initiate() {
            let meatInfo = getMetaInfo();
            this.formData.metaInfo = JSON.stringify(meatInfo);
            this.formData.id=Cookies.get('id');
            axios({
                url: baseUrl + '/signApp/customer/initFaceVerify',
                headers: {
                    "Content-Type": "application/json",
                },
                method: 'post',
                data: this.formData,

            }).then((res) => {
                let data = res.data;
                if (data.code == 200) {
                    window.location.href = data.resultObject.certifyUrl;
                } else {
                    alert(data.message)
                }
            }).catch((err) => {
                console.log(err);

            });
        },


    },
})

